package com.prp.gwt.client.widgets.user.friend;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.IListener;
import com.prp.gwt.client.user.friend.FriendListener;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.vo.User;
import com.prp.gwt.client.widgets.ConfirmDialog;
import com.prp.gwt.client.widgets.IWidget;

public class FriendItem extends HorizontalPanel implements IWidget {

	private User friend;
	private VerticalPanel operationPanel = new VerticalPanel();
	private HTML buildup = new HTML("<a>成为邻友</a>");
	
	public FriendItem(User friend) {
		this.friend = friend;
		Image portrait = new Image(friend.getPortrait());
		portrait.setWidth("50px");
		add(portrait);
		add(getInfoPanel());
		add(getOperationPanel());
		addStyleName("friend-item");
	}

	private VerticalPanel getInfoPanel() {
		VerticalPanel panel = new VerticalPanel();
		panel.setSpacing(15);
		panel.setWidth("200px");
		HTML userName = new HTML("<a>"+friend.getUsername()+"</a>");
		userName.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(PHPHistoryHandler.PREFIX_USERHOME+friend.getId());
			}
		});
		panel.add(userName);
		userName.addStyleName("friend-item-text");
		return panel;
	}

	private VerticalPanel getOperationPanel() {
		operationPanel.setSpacing(5);
		HTML mail = new HTML("<a>发送站内信</a>");
		HTML delete = new HTML("<a>解除邻友关系</a>");
		buildup.addStyleName("friend-item-text");
		mail.addStyleName("friend-item-text");
		delete.addStyleName("friend-item-text");
		operationPanel.add(mail);
		final User sender = FriendListPanel.getInstance().sender;
		final User owner = FriendListPanel.getInstance().owner;
		if (sender.equals(owner) || sender.getIdentity() > ClientConstants.USER)
			operationPanel.add(delete);
		if (!sender.equals(owner)) {
			operationPanel.add(buildup);
			buildup.setVisible(false);
			FriendListener.getInstance().isNeighbour(sender.getId(),
					friend.getId(), this);
		}
		delete.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ConfirmDialog.getInstance().setCallFunction(FriendItem.this,
						null);
				String title = "确认解除邻友关系";
				if (sender.equals(owner))
					ConfirmDialog.getInstance()
							.show(
									title,
									"您要和" + friend.getUsername()
											+ "解除邻友关系吗?<br/><br/>");
				else
					ConfirmDialog.getInstance()
							.show(
									title,
									"是否真的要解除" + owner.getUsername() + "和"
											+ friend.getUsername()
											+ "的邻友关系?<br/><br/>");
			}
		});
		return operationPanel;
	}

	public void addBuildupOperation() {
		buildup.setVisible(true);
		buildup.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				FriendListener.getInstance().invite(friend);
			}
		});
	}

	@Override
	public void callFunction(Object arg) {
		FriendListener.getInstance().breakup(
				FriendListPanel.getInstance().owner.getId(), friend.getId());
	}

	@Override
	public void setListener(IListener listener) {
		// do nothing
	}
}
